package com.xiaoqiudong.forum.controller;

import com.xiaoqiudong.forum.common.AppResult;
import com.xiaoqiudong.forum.common.ResultCode;
import com.xiaoqiudong.forum.config.AppConfig;
import com.xiaoqiudong.forum.model.Article;
import com.xiaoqiudong.forum.model.ArticleReply;
import com.xiaoqiudong.forum.model.User;
import com.xiaoqiudong.forum.service.IArticleReplyService;
import com.xiaoqiudong.forum.service.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-08-28
 * Time:9:48
 */
@Slf4j
@Tag(name = "回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleReplyService articleReplyService;
    @Resource
    private IArticleService articleService;
    @PostMapping("/create")
    @Operation(description = "回复贴子")
    public AppResult create(HttpServletRequest request,
                            @Parameter(name = "articleId" ,description = "帖子id") @RequestParam("articleId") @Nonnull Long articleId,
                            @Parameter(name = "content" ,description = "回复类容") @RequestParam("content") @Nonnull String content){
        //获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否禁言
        if (user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        //获取回复帖子
        Article article = articleService.selectById(articleId);
        //帖子不存在
        if (article == null || article.getDeleteState() == 1){

            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //是否封贴
        if (article.getState() == 1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReply.setArticleId(article.getId());
        //写入数据库
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @Operation(description = "回复帖子列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getReplies(@Parameter(name = "articleId" ,description = "帖子id") @RequestParam("articleId") @Nonnull Long articleId){
        //检验贴子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        log.info("查询成功");
        return AppResult.success(articleReplies);
    }
}
